package com.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.DotBean;
import com.bean.MessageBean;
import com.bean.UserBean;
import com.bean.UserLoginBean;
import com.dao.UserDao;
import com.exception.DBException;
import com.exception.UserException;

public class UserAction
{
	/**
	 * if user id is repeated, present the error. automaticly generate username
	 * ,it is unique. transaction, add user information to
	 * user,userDot,userLogin, if failed, three transaction will rollback. if
	 * this user has already existed, and back information to user
	 * 
	 * @param request
	 * @param response
	 * @param userBean
	 * @throws DBException
	 * @throws UserException
	 * @throws UserTypeException
	 */
	public void addUser(HttpServletRequest request,
			HttpServletResponse response, UserBean userBean)
			throws DBException, UserException
	{
		UserDao userDao = new UserDao();
		// if user is not exist and userTypename is exist
		if (userDao.ifUserExist(userBean))
		{
			throw new UserException("user is already exist");
		}
		DotBean proBean = new DotBean();
		proBean.setUserID(userBean.getUserID());
		proBean.setContext("");

		UserLoginBean userLoginBean = new UserLoginBean();
		userLoginBean.setUsername(userBean.getUserID());
		userLoginBean.setUserID(userBean.getUserID());
		userLoginBean.setPassword("000000"); // defult password

		// transaction
		userDao.addUser(userBean, userLoginBean, proBean);

		userDao.getUserDetails(userBean);
		// after add,return to the details web page. and present details the
		// user who info have changed
		request.setAttribute("userdetails", userBean);

		try
		{
			request.getRequestDispatcher("/lab_admin/user_detail.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

	private ArrayList<UserBean> query_Key_Value(MessageBean message)
			throws DBException, UserException
	{
		UserBean userBean = new UserBean();
		UserDao userDao = new UserDao();
		ArrayList<UserBean> userList = null;
		String key = message.getKey();
		String value = message.getValue();
		// query users by id
		if ("userid".equals(key))
		{
			userBean.setUserID(value);
			userList = userDao.getUsersByID(userBean);
		}
		// query users by firstname
		else if ("firstname".equals(key))
		{
			userBean.setFirstname(value);
			userList = userDao.getUserByFirstname(userBean);
		}
		// query users by lastname
		else if ("lastname".equals(key))
		{
			userBean.setLastname(value);
			userList = userDao.getUserByLastName(userBean);
		}
		// otherwise set user list is nukk
		else
			userList = null;
		return userList;
	}

	/**
	 * message is a bean. it is contain 2 field. one is key. annother is value
	 * key is the depending. value is the content will be queried
	 * 
	 * @param request
	 * @param response
	 * @param message
	 * @throws DBException
	 * @throws UserException
	 */
	public void queryUsers(HttpServletRequest request,
			HttpServletResponse response, MessageBean message)
			throws DBException, UserException
	{

		ArrayList<UserBean> userList = null;
		userList = query_Key_Value(message);
		request.setAttribute("users", userList);
		request.setAttribute("key", message);
		try
		{
			request.getRequestDispatcher("/lab_admin/user.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void deleteUser(HttpServletRequest request,
			HttpServletResponse response, UserBean userBean, MessageBean message)
			throws DBException, UserException
	{
		UserDao userDao = new UserDao();
		if (!userDao.ifUserExist(userBean))
		{
			throw new UserException("user is not exist");
		}
		ArrayList<UserBean> userList = null;
		userDao.deleteUser(userBean);
		// reload page after delete the user. page will not present the user
		// deleting
		userList = query_Key_Value(message);
		request.setAttribute("users", userList);
		request.setAttribute("key", message);
		try
		{
			request.getRequestDispatcher("/lab_admin/user.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void modifyUser(HttpServletRequest request,
			HttpServletResponse response, UserBean userBean)
			throws DBException, UserException
	{
		UserDao userDao = new UserDao();
		if (!userDao.ifUserExist(userBean))
		{
			throw new UserException("user is not exist");
		}
		userDao.updateUser(userBean);

		userDao.getUserDetails(userBean);
		// after update,return the query web page. and present details the user
		// who info have changed
		request.setAttribute("userdetails", userBean);

		try
		{
			request.getRequestDispatcher("/lab_admin/user_detail.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * from the table edit to modify user info web page all information will be
	 * forwarded to modify web page
	 * 
	 * @param request
	 * @param response
	 * @param userBean
	 * @throws DBException
	 * @throws UserException
	 * @throws UserTypeException
	 */

	/*
	 * click edit the user info, all the user type and user info will be present
	 * the web page
	 */
	public void modifyUserInfo(HttpServletRequest request,
			HttpServletResponse response, UserBean userBean)
			throws DBException, UserException
	{
		UserDao userDao = new UserDao();
		if (!userDao.ifUserExist(userBean))
		{
			throw new UserException("user is not exist");
		}
		userDao.getUserDetails(userBean);

		request.setAttribute("modifyuserinfo", userBean);
		try
		{
			request.getRequestDispatcher("/lab_admin/user_modify.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void detailsUser(HttpServletRequest request,
			HttpServletResponse response, UserBean userBean)
			throws DBException, UserException
	{
		UserDao userDao = new UserDao();
		if (!userDao.ifUserExist(userBean))
		{
			throw new UserException("user is not exist");
		}
		userDao.getUserDetails(userBean);
		request.setAttribute("userdetails", userBean);
		try
		{
			request.getRequestDispatcher("/lab_admin/user_detail.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
